.PHONY: test
WCT_PATH = ./node_modules/web-component-tester/bin/wct
VULCANIZE_PATH = ./node_modules/vulcanize/bin/vulcanize
GAE_PY?= ../../../../../../gcloud/bin/dev_appserver.py

default: help

help:
	@echo "Available commands:"
	@sed -n '/^[a-zA-Z0-9_]*:/s/:.*//p' <Makefile

build:
	cd frontend && make build
	cd som/model && go generate

clean:
	cd frontend && make clean

format:
	gofmt -s -w .
	cd frontend && make format

test: build
	cd som && go test infra/appengine/sheriff-o-matic/som/... -count 10 -failfast -cover -race
	cd frontend && make wct

relnotes:
	go run ../../tools/relnotes/relnotes.go -app sheriff-o-matic -extra-paths .,../../monitoring/analyzer,../../monitoring/client,../../monitoring/messages

# This is only used for testing on the continuous build system.
bower_cipd: deps
	cipd pkg-build -pkg-def ./cipd/sheriff-o-matic.yaml -pkg-var platform:linux-amd64 -out ./cipd/bower_components.cipd
	cipd pkg-register ./cipd/bower_components.cipd

getversion_prod:
	$(eval VERS := $(shell ./gae.py version -p ./frontend -A sheriff-o-matic | cut -c 1-35))
	echo version is $(VERS)

getversion_staging:
	$(eval VERS := $(shell ./gae.py version -p ./frontend -A sheriff-o-matic-staging | cut -c 1-35))
	echo version is $(VERS)

deploy_prod: build getversion_prod
	gcloud app deploy --no-promote --no-stop-previous-version frontend/app.yaml backend/app.yaml dispatch.yaml cron.yaml frontend/queue.yaml --project sheriff-o-matic --version $(VERS)
	rm frontend/elements/som-app/som-app.vulcanized.html

bqtools:
	go install go.chromium.org/luci/tools/cmd/bqschemaupdater

update_bq_staging: bqtools
	bqschemaupdater -proto-dir ./som/model/gen -table sheriff-o-matic-staging.events.alerts -message gen.SOMAlertsEvent
	bqschemaupdater -proto-dir ./som/model/gen -table sheriff-o-matic-staging.events.annotations -message gen.SOMAnnotationEvent

update_bq_prod: bqtools
	bqschemaupdater -proto-dir ./som/model/gen -table sheriff-o-matic.events.alerts -message gen.SOMAlertsEvent
	bqschemaupdater -proto-dir ./som/model/gen -table sheriff-o-matic.events.annotations -message gen.SOMAnnotationEvent

deploy_staging: build getversion_staging
	gcloud app deploy --no-promote --no-stop-previous-version frontend/app.yaml backend/app.yaml dispatch.yaml cron.yaml frontend/queue.yaml --project sheriff-o-matic-staging --version $(VERS)
	rm frontend/elements/som-app/som-app.vulcanized.html

devserver:
	$(GAE_PY) frontend/app.yaml backend/app.yaml

# The -remote commands expose the app so that it is externally
# accessible by hostname:port, rather than just localhost:port.
devserver_remote:
	$(GAE_PY) --host $$HOSTNAME frontend/app.yaml backend/app.yaml
